﻿Imports System.IO

Partial Class photoalbums_admin_editAlbum
    Inherits System.Web.UI.Page

    Private _AlbumID As Integer
    Public Property AlbumID() As Integer
        Get
            Return _AlbumID
        End Get
        Set(ByVal value As Integer)
            _AlbumID = value
        End Set
    End Property

    Private _itemID As Integer
    Public Property itemID() As Integer
        Get
            Return _itemID
        End Get
        Set(ByVal value As Integer)
            _itemID = value
        End Set
    End Property

    Private _btnDeleteImage As LinkButton
    Public Property btnDeleteImage() As LinkButton
        Get
            Return _btnDeleteImage
        End Get
        Set(ByVal value As LinkButton)
            _btnDeleteImage = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AlbumID = Request.QueryString("albumID")
    End Sub



    Protected Sub Click_isPrivate(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim albumAdapter As New MainDbTableAdapters.AlbumsTableAdapter()
        Dim chkIsPrivate As CheckBox = CType(detAlbum.FindControl("chkPrivate"), CheckBox)


        albumAdapter.UpdateAlbumPrivate(chkIsPrivate.Checked, AlbumID)
        lblCheck.Text = chkIsPrivate.Checked


        detAlbum.DataBind()
    End Sub

    Protected Sub Click_DeleteImage(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim albumAdapter As New MainDbTableAdapters.AlbumImagesTableAdapter()
        Dim commandArgs As Array

        commandArgs = Split(btnDeleteImage.CommandArgument, ",")

        'albumAdapter.Delete(btnDeleteImage.CommandArgument)

        For Each item As String In commandArgs
            lblCheck.Text += item & "  "
        Next


    End Sub



    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim imageAdapter As New MainDbTableAdapters.ImagesTableAdapter()

        lstImages.DataSource = imageAdapter.GetImagesByAlbumID(AlbumID, User.Identity.Name)
        lstImages.DataBind()

    End Sub

    Protected Sub lstImages_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstImages.SelectedIndexChanged
        Dim imageAdapter As New MainDbTableAdapters.ImagesTableAdapter()
        Dim aiAdapter As New MainDbTableAdapters.AlbumImagesTableAdapter()
        Dim commandArgs As Array
        Dim fileDir As String

        btnDeleteImage = CType(lstImages.Items(itemID).FindControl("btnDeleteImage"), LinkButton)
        commandArgs = Split(btnDeleteImage.CommandArgument, ",")

        fileDir = Server.MapPath("/img/imgGallery/")

        File.Delete(fileDir & commandArgs(2))

        aiAdapter.Delete(commandArgs(0))
        imageAdapter.Delete(commandArgs(1))

        lblCheck.Text = fileDir



    End Sub

    Protected Sub lstImages_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSelectEventArgs) Handles lstImages.SelectedIndexChanging
        itemID = e.NewSelectedIndex
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSubmit.Click
        Dim uploads As HttpFileCollection
        uploads = HttpContext.Current.Request.Files

        Dim imageAdapter As New MainDbTableAdapters.ImagesTableAdapter()
        Dim aiAdapter As New MainDbTableAdapters.AlbumImagesTableAdapter()
        Dim ImgID As Integer
        Dim imgName As String
        Dim imgCap As String
        Dim filePath As String
        Dim imgHeight As Integer = 0
        Dim imgWidth As Integer = 0
        imgName = txtImgName.Text
        imgCap = txtCaption.Text


        For i As Integer = 0 To (uploads.Count - 1)

            If (uploads(i).ContentLength > 0) Then
                Dim fileName As String = System.IO.Path.GetFileName(uploads(i).FileName)

                filePath = "/img/photogal/" & fileName


                uploads(i).SaveAs("C:\inetpub\wwwroot\Henson Online\img\imgGallery\" & fileName)
                lblMessage.Text = "File Uploaded Sucessfully."
                ImgID = imageAdapter.InsertImage(User.Identity.Name, imgName, imgCap, fileName, imgHeight, imgWidth, False)
                aiAdapter.Insert(AlbumID, ImgID)

                Try

                Catch Exp As Exception
                    lblMessage.Text = "Some Error occured."
                End Try

            End If

        Next i

        Response.Redirect("editAlbum.aspx?AlbumID=" & AlbumID)
    End Sub

End Class
